package cn.rubbit.entity;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Project extends BaseBean {
	private String artifactId;
	private String groupId;
	private String name;
	private List<Dependency> dependencies;
	private TYPE type;
	private String path;
	private List<Table> tables = new ArrayList<Table>();

	public List<Table> getTables() {
		return tables;
	}

	public void setTables(String ...names) {
		for(String tablename: Arrays.asList(names)){
			Table tem=new Table();
			tem.setTable(tablename);
			tables.add(tem);
		}
	}

	public String getPath() {
		return path;
	}

	public void setPath(String path) {
		this.path = path;
	}

	public String getType() throws Exception {
		if (type == TYPE.JAR) {
			return "jar";
		} else {
			return "war";
		}
	}

	public void setType(TYPE type) {
		this.type = type;
	}

	public String getArtifactId() {
		return artifactId;
	}

	public void setArtifactId(String artifactId) {
		this.artifactId = artifactId;
	}

	public String getGroupId() {
		return groupId;
	}

	public void setGroupId(String groupId) {
		this.groupId = groupId;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public List<Dependency> getDependencies() {
		return dependencies;
	}

	public void setDependencies(List<Dependency> dependencies) {
		this.dependencies = dependencies;
	}

	@SuppressWarnings("unchecked")
	@Override
	public Map<String, Object> toMap() {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("name", name);
		map.put("groupId", groupId);
		map.put("artifactId", artifactId);
		try {
			map.put("type", getType());
		} catch (Exception e) {
			e.printStackTrace();
		}
		Map<String, Object>[] deps = null;
		if (this.dependencies != null && this.dependencies.size() > 0) {
			deps = new Map[dependencies.size()];
			for (int i = 0; i < dependencies.size(); i++) {
				deps[i] = dependencies.get(i).toMap();
			}
		}
		map.put("dependencies", deps);
		return map;
	}

	public enum TYPE {
		JAR, WAR
	};

}
